package com.keshe.cart.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.keshe.cart.entity.CouponEntity;
import com.keshe.cart.service.CouponService;
import com.keshe.common.utils.PageUtils;
import com.keshe.common.utils.R;



/**
 *
 *
 * @author xiongsiyi
 * @email 2825889385@qq.com
 * @date 2023-01-02 14:12:01
 */
@RefreshScope
@RestController
@RequestMapping("cart/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("cart:coupon:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查找
     */
    @RequestMapping("/find/{cid}")
    //@RequiresPermissions("cart:coupon:find")
    public R find(@RequestParam Map<String, Object> params, @PathVariable("cid") int cid){
//        PageUtils page = couponService.queryPage(params);
//        IPage pp = (IPage) page;
//        ArrayList<CouponEntity> couponEntities = (ArrayList<CouponEntity>) pp;
//        couponEntities.removeIf(coupon -> coupon.getCid() != params.get("cid"));
//
//        Page<CouponEntity> p = new PageImpl<CouponEntity>(couponEntities, (Pageable) params, page.getTotalCount());
//        PageRequest pageRequest = null;
//        pageRequest.setCurrent(page.getCurrPage());
//        pageRequest.setSize(page.getPageSize());
//        Page p = (Page) PageUtils.toPage(couponEntities, pageRequest);
//        PageUtils pageUtils = (PageUtils) p;
        PageUtils page = couponService.queryPage(params, cid);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("cart:coupon:info")
    public R info(@PathVariable("id") Integer id){
		CouponEntity coupon = couponService.getById(id);

        return R.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("cart:coupon:save")
    public R save(@RequestBody CouponEntity coupon){
		couponService.save(coupon);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("cart:coupon:update")
    public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("cart:coupon:delete")
    public R delete(@RequestBody Integer[] ids){
		couponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
